package cn.hy.listener;

import cn.hy.entity.Order;
import cn.hy.enums.OrderStatusEnum;
import cn.hy.events.OrderStatusChangeEvent;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Service
@WithStateMachine(name = "orderStatusMachine")
public class OrderStatusListener {

    private static final String STR_ORDER = "order";

    // 待付款 -> 部分支付 部分支付事件
    @OnTransition(source = "PAY_WAIT", target = "PAY_PART")
    public boolean payWaitToPayPartEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.PAY_PART.getCode());
            return true;
        }
        return false;
    }

    // 待付款 -> 已支付(待成团) 全部支付，待成团
    @OnTransition(source = "PAY_WAIT", target = "PAY_OVER")
    public boolean payWaitToPayOverEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.PAY_OVER.getCode());
            return true;
        }
        return false;
    }

    // 待付款 -> 待发货(待授权) 实体全部付款待发货，虚拟全部付款待授权
    @OnTransition(source = "PAY_WAIT", target = "SEND_WAIT")
    public boolean payWaitToSendWaitEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_WAIT.getCode());
            return true;
        }
        return false;
    }

    // 待付款 -> 已完成(全部收货) 不需要发货
    @OnTransition(source = "PAY_WAIT", target = "FINISHED")
    public boolean payWaitToFinishedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.FINISHED.getCode());
            return true;
        }
        return false;
    }

    // 待付款 -> 已关闭 订单取消
    @OnTransition(source = "PAY_WAIT", target = "CLOSED")
    public boolean payWaitToClosedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.CLOSED.getCode());
            return true;
        }
        return false;
    }


    // 部分支付 -> 已支付(待成团) 拼团订单支付剩下的一部分
    @OnTransition(source = "PAY_PART", target = "PAY_OVER")
    public boolean payPartToPayOverEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.PAY_OVER.getCode());
            return true;
        }
        return false;
    }

    // 部分支付 -> 待发货(待授权) 订单支付剩下的一部分
    @OnTransition(source = "PAY_PART", target = "SEND_WAIT")
    public boolean payPartToSendWaitEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_WAIT.getCode());
            return true;
        }
        return false;
    }

    // 部分支付 -> 已完成(全部收货) 不需要发货订单支付剩下的一部分
    @OnTransition(source = "PAY_PART", target = "FINISHED")
    public boolean payPartToFinishedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.FINISHED.getCode());
            return true;
        }
        return false;
    }


    // 已支付(待成团) -> 待发货(待授权) 拼团订单拼团成功
    @OnTransition(source = "PAY_OVER", target = "SEND_WAIT")
    public boolean payOverToSendWaitEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_WAIT.getCode());
            return true;
        }
        return false;
    }

    // 已支付(待成团) -> 已完成(全部收货) 不需要发货的订单拼团成功
    @OnTransition(source = "PAY_OVER", target = "FINISHED")
    public boolean payOverToFinishedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.FINISHED.getCode());
            return true;
        }
        return false;
    }

    // 已支付(待成团) -> 发起售后申请 拼团订单未拼成功发起售后申请
    @OnTransition(source = "PAY_OVER", target = "AFTERMARKET_APPLY")
    public boolean payOverToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }


    // 待发货(待授权) -> 部分发货
    @OnTransition(source = "SEND_WAIT", target = "SEND_PART")
    public boolean sendWaitToSendPartEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_PART.getCode());
            return true;
        }
        return false;
    }

    // 待发货(待授权) -> 已发货(全部发货)
    @OnTransition(source = "SEND_WAIT", target = "SEND_OVER")
    public boolean sendWaitToSendOverEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_OVER.getCode());
            return true;
        }
        return false;
    }

    // 待发货(待授权) -> 发起售后申请
    @OnTransition(source = "SEND_WAIT", target = "AFTERMARKET_APPLY")
    public boolean sendWaitToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }


    // 部分发货 -> 已发货(全部发货)
    @OnTransition(source = "SEND_PART", target = "SEND_OVER")
    public boolean sendPartToSendOverEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.SEND_OVER.getCode());
            return true;
        }
        return false;
    }

    // 部分发货 -> 发起售后申请
    @OnTransition(source = "SEND_PART", target = "AFTERMARKET_APPLY")
    public boolean sendPartToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }


    // 已发货(全部发货) -> 已完成(全部收货)
    @OnTransition(source = "SEND_OVER", target = "FINISHED")
    public boolean sendOverToFinishedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.FINISHED.getCode());
            return true;
        }
        return false;
    }

    // 已发货(全部发货) -> 发起售后申请
    @OnTransition(source = "SEND_OVER", target = "AFTERMARKET_APPLY")
    public boolean sendOverToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }


    // 已完成(全部收货) -> 发起售后申请
    @OnTransition(source = "FINISHED", target = "AFTERMARKET_APPLY")
    public boolean finishedToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }


    // 发起售后申请 -> 部分退款
    @OnTransition(source = "AFTERMARKET_APPLY", target = "REFUND_PART")
    public boolean aftermarketApplyToRefundPartEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.REFUND_PART.getCode());
            return true;
        }
        return false;
    }

    // 发起售后申请 -> 已关闭
    @OnTransition(source = "AFTERMARKET_APPLY", target = "CLOSED")
    public boolean aftermarketApplyToClosedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.CLOSED.getCode());
            return true;
        }
        return false;
    }


    // 部分退款 -> 已关闭
    @OnTransition(source = "REFUND_PART", target = "CLOSED")
    public boolean refundPartToClosedEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.CLOSED.getCode());
            return true;
        }
        return false;
    }

    // 部分退款 -> 发起售后申请 再次发起售后申请
    @OnTransition(source = "REFUND_PART", target = "AFTERMARKET_APPLY")
    public boolean refundPartToAftermarketApplyEvent(Message<OrderStatusChangeEvent> message) {
        Order order = (Order) message.getHeaders().get(STR_ORDER);
        if (!Objects.isNull(order)) {
            order.setStatus(OrderStatusEnum.AFTERMARKET_APPLY.getCode());
            return true;
        }
        return false;
    }

}
